package com.laolu.icon.utils;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Slf4j
public class MyRegistry {
	// 注册表路径和键名
	private static final String REG_KEY_PATH = "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced";
	private static final String SHOW_HIDDEN_KEY = "Hidden";
	private static final String SHOW_PROTECTED_KEY = "ShowSuperHidden";
	private static final String COMMOND = "reg query %s /v %s";
	
	
	/**
	 * 是否显示隐藏文件
	 * @return
	 */
	public boolean isShowHiddenFile() {
		Integer hidden = isRegistryValueEnabled(SHOW_HIDDEN_KEY);
		return hidden != null && hidden == 1;
	}
	
	/**
	 * 是否显示受保护的系统文件
	 * @return
	 */
	public boolean isShowProtectedFile() {
		Integer hidden = isRegistryValueEnabled(SHOW_PROTECTED_KEY);
		return hidden != null && hidden == 1;
	}
	
	
	/**
	 * 读取注册表键值（DWORD 类型）
	 * @param valueName
	 * 		键名（如 "Hidden"）
	 * @return 键值是否为 1（开启）
	 */
	private @Nullable Integer isRegistryValueEnabled(String valueName) {
		try {
			// 执行 reg query 命令
			String cmd = String.format(COMMOND, REG_KEY_PATH, valueName);
			
			ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", cmd);
			Process process = processBuilder.start();
			
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			
			// 解析输出
			String line;
			while ((line = reader.readLine()) != null) {
				if (line.contains("REG_DWORD")) {
					String[] parts = line.trim().split("\\s+");
					
					return Integer.parseInt(parts[parts.length - 1].replace("0x", ""), 16);
				}
			}
			
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}
}
